package factory;

import java.util.ArrayList;
import java.util.List;

import util.Log;
import base.Item;

public class Machine extends Item {

	private boolean busy = true;
	private double readyTime;
	private double totalWorkingTime;
	private double endTime;
	private double startTime =0.0d;

	private int step;
	private List<Job> machineSchedule = new ArrayList<Job>();
	private int index;
	

	public Machine() {
		release();
	}

	private OnCompleteListenner onCompleteListenner;

	public void setOnCompleteListenner(OnCompleteListenner onCompleteListenner) {
		this.onCompleteListenner = onCompleteListenner;
	}

	public boolean isBusy() {
		return busy;
	}

	public void setReadyTime(double readyTime) {
		this.readyTime = readyTime;
	}

	public double getTotalWorkingTime() {
		return totalWorkingTime;
	}

	public void setTotalWorkingTime(double totalWorkingTime) {
		this.totalWorkingTime = totalWorkingTime;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public int getStep() {
		return step;
	}
	
	@SuppressWarning
	public void run(Job job) {

		busy = true;
		machineSchedule.add(job);
		
	//	Log.debug("================================================머신 " +this.getIndex() +" 잡 인덱스 "+ job.getJobIndex() + " 잡 시간  " +job.getProcessingTime() );
		endTime += job.getProcessingTime();
		setReadyTime(job.getProcessingTime());
		job.setFinish(true);
		step++;
	}

	public void onComplete() {
		busy = false;
		totalWorkingTime += readyTime;
	//	Log.debug("레디타임 추가 후 워킹 타임  "+totalWorkingTime);
		
		if (onCompleteListenner == null)
			return;
		onCompleteListenner.onComplete(this); // 머신 매니저의 onComplete 호출
	}

	@Override
	public void release() {
		busy = false;
		readyTime = 0;
		step = 0;
		totalWorkingTime = 0;
		setName("Machine");
		setValue(String.valueOf(totalWorkingTime));
		super.release();
	}

	public double getReadyTime() {
		return readyTime;
	}

	public int getIndex() { 
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public List<Job> getMachineSchedule() {
		return machineSchedule;
	}

	public void setMachineSchedule(List<Job> machineSchedule) {
		this.machineSchedule = machineSchedule;
	}

	public void setBusy(boolean busy) {
		this.busy = busy;
	}
	
	public interface OnCompleteListenner {
		public abstract void onComplete(Machine machine);

	}

	public double getEndTime() {
		return endTime;
	}

	public void setEndTime(double endTime) {
		this.endTime = endTime;
	}
	public double getStarTime() {
		return startTime;
	}

	public void setStarTime(double starTime) {
		this.startTime = starTime;
	}
	
}